package com.itbour.service.trade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.dao.trade.ActivityInfoDao;
import com.itbour.dto.trade.ActivityInfoDto;
import com.itbour.dto.trade.ActivityRInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PriceInfoDto;
import com.itbour.dto.trade.PrintRelInfoDto;
import com.itbour.service.trade.ActivityInfoService;

public class ActivityInfoServiceImpl implements ActivityInfoService {
	
	private ActivityInfoDao activityInfoDao;
	
	//获取活动
	public Page getActivityInfo(Map<String, Object> parm,Integer pageNo,Integer pageSize) {
		Page result = null;
		// 实际起始位置
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = activityInfoDao.getActivityCnt(parm);
		
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		// 分页检索
		parm.put("pageSize", pageSize);
		parm.put("skipResults", skipResults);
		List<ActivityInfoDto> data=activityInfoDao.listActivityInfo(parm);
		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}
	
	//获取活动商品信息
	public Page getActItemInfo(Map<String, Object> parm,Integer pageNo,Integer pageSize){
		Page result = null;
		int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
		int totalSize = activityInfoDao.selectActItemCnt(parm);
		while(totalSize >0 && totalSize <= skipResults){
			skipResults= skipResults - pageSize;
			pageNo = pageNo -1;
		}
		parm.put("pageSize", pageSize);
		parm.put("skipResults", skipResults);
		List<ActivityRInfoDto> data=activityInfoDao.selectActItemInfo(parm);
		result = new JspPage(skipResults, totalSize, pageSize, data);
		return result;
	}

	//获取商品
	public Page itemsPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = activityInfoDao.selectItemsCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<ItemInfoDto> data = activityInfoDao.selectItemsInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	//插入活动
	public void insertActivityInfo(ActivityInfoDto ac) {
		Map<String, Object> parm=new HashMap<String, Object>();
		parm.put("type",ac.getType());
		parm.put("title",ac.getTitle());
		parm.put("start_time",ac.getStart_time());
		parm.put("end_time",ac.getEnd_time());
		parm.put("desc",ac.getDesc());
		parm.put("image",ac.getImage());
		parm.put("url",ac.getUrl());
		parm.put("update_by",ac.getUpdate_by());
		activityInfoDao.insertActivityInfo(parm);
	}

	
	public void updateActivityInfo(ActivityInfoDto ac) {
		Map<String, Object> parm=new HashMap<String, Object>();
		if(ac.getAct_id()!=null){
			parm.put("act_id",ac.getAct_id());
		}
		if(ac.getType()!=null){
			parm.put("type",ac.getType());
		}
		if(ac.getTitle()!=null){
			parm.put("title",ac.getTitle());
		}
		if(ac.getStart_time()!=null){
			parm.put("start_time",ac.getStart_time());
		}
		if(ac.getEnd_time()!=null){
			parm.put("end_time",ac.getEnd_time());
		}
		if(ac.getDesc()!=null){
			parm.put("desc",ac.getDesc());
		}
		if(ac.getImage()!=null){
			parm.put("image",ac.getImage());
		}
		if(ac.getUpdate_by()!=null){
			parm.put("update_by",ac.getUpdate_by());
		}if(ac.getUrl()!=null){
			parm.put("url",ac.getUrl());
		}
		if(ac.getOrder()!=null){
			parm.put("order",ac.getOrder());
		}
		activityInfoDao.updateActivityInfo(parm);
	}

	public ActivityInfoDao getActivityInfoDao() {
		return activityInfoDao;
	}
	public void setActivityInfoDao(ActivityInfoDao activityInfoDao) {
		this.activityInfoDao = activityInfoDao;
	}

	public List<ActivityInfoDto> getOneActivityInfo(Map<String, Object> parm) {
		parm.put("pageSize", 1);
		parm.put("skipResults", 0);
		return activityInfoDao.listActivityInfo(parm);
	}

	public void delActivityInfo(Map<String, Object> parm) {
		activityInfoDao.delActivityInfo(parm);
	}

	@Override
	public void publishActInfo(Map<String, Object> parm) {
		activityInfoDao.publishActInfo(parm);
	}

	//获取套餐
	public List<PrintRelInfoDto> getItemComboInfo(Map<String, Object> parm) {
		return activityInfoDao.selectPrintRelInfoByItem(parm);
	}

	//获取套餐价格计量
	public List<PriceInfoDto> getQtyPriceInfo(Map<String, Object> parm) {
		return activityInfoDao.selectNumberByPpId(parm);
	}

	//添加活动商品
	public void insertActivityItemInfo(ActivityRInfoDto acr) {
		Map<String, Object> parm=new HashMap<String, Object>();
		parm.put("act_id",acr.getAct_id());
		parm.put("item_id",acr.getItem_id());
		parm.put("pp_id",acr.getPp_id());
		parm.put("item_qty",acr.getItem_qty());
		parm.put("item_unit",acr.getItem_unit());
		parm.put("org_price",acr.getOrg_price());
		parm.put("act_price",acr.getAct_price());
		parm.put("qty",acr.getQty());
		parm.put("rest_qty",acr.getRest_qty());
		parm.put("expiry_time",acr.getExpiry_time());
		parm.put("desc",acr.getDesc());
		activityInfoDao.insertActivityRInfo(parm);
	}
	
	//删除活动商品
	public void delActivityRInfo(Map<String, Object> parm) {
		activityInfoDao.delActivityRInfo(parm);
	}

	@Override
	public List<ActivityRInfoDto> getOneActivityRInfo(Map<String, Object> parm) {
		return activityInfoDao.getOneActivityRInfo(parm);
	}

	@Override
	public void updateActivityRInfo(ActivityRInfoDto acr) {
		Map<String, Object> parm=new HashMap<String, Object>();
		if(acr.getR_id()==null|| acr.getR_id().equals(" ")){
			return;
		}
		parm.put("r_id",acr.getR_id());
		parm.put("act_id",acr.getAct_id());
		parm.put("item_id",acr.getItem_id());
		parm.put("pp_id",acr.getPp_id());
		parm.put("item_qty",acr.getItem_qty());
		parm.put("item_unit",acr.getItem_unit());
		parm.put("org_price",acr.getOrg_price());
		parm.put("act_price",acr.getAct_price());
		parm.put("qty",acr.getQty());
		parm.put("rest_qty",acr.getRest_qty());
		parm.put("expiry_time",acr.getExpiry_time());
		parm.put("desc",acr.getDesc());
		activityInfoDao.updateActivityRInfo(parm);
	}
}
